package logic

import (
	"context"
	"errors"
	"lucky_srv/cache"
	"lucky_srv/models"

	"lucky_srv/internal/svc"
	"lucky_srv/lucky"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelCartLogic {
	return &DelCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelCartLogic) DelCart(in *lucky.DelCartRequest) (*lucky.DelCartResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var u models.User
	if err = u.GetUserById(in.UserId); err != nil {
		return nil, errors.New("用户信息获取失败")
	}
	if u.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	if !cache.IsExistCart(in.UserId, in.GoodsId) {
		logx.Error("要删除的商品不存在")
		return nil, errors.New("要删除的商品不存在")
	}

	if err = cache.DelCart(in.UserId, in.GoodsId); err != nil {
		logx.Error("购物车商品删除失败")
		return nil, errors.New("购物车商品删除失败")
	}

	return &lucky.DelCartResponse{}, nil
}
